tinyMCEPopup.requireLangPack();

var StyleLibraryTextBoxDialog = {
	init : function() {
		var f = document.forms[0];

		document.getElementById('previewbg_pickcontainer').innerHTML = getColorPickerHTML('previewbg_pick','previewbg');
		
		fntFont.init(tinyMCEPopup.editor);
		cellProp.init(tinyMCEPopup.editor);
		BorderPicker.init(tinyMCEPopup.editor);
		//mrgMargin.init(tinyMCEPopup.editor);
		
		TinyMCE_EditableSelects.init();

		initDesigner("dialog");
	}
};

tinyMCEPopup.onInit.add(StyleLibraryTextBoxDialog.init, StyleLibraryTextBoxDialog);

function initDesigner(caller)
{
	// Run only if all loaded
	switch(caller) {
		case "frame":
			frameLoaded = true;
			break;
		case "dialog":
			dialogLoaded = true;
			break;
		case "css":
			if(tinymce.isGecko) { return; }  // Never runs. Gecko does not call this way.
			cssLoaded = true;
			break;
	}
	if(!frameLoaded || !dialogLoaded || (!tinymce.isGecko && !cssLoaded)) {
		return;
	}

	// Configure which rules allow what properties.
	// TextBox DIV
	designer.rules[0].properties.initBorder();
	designer.rules[0].properties.initFont();
	designer.rules[0].properties.initCellProp();
	designer.rules[0].properties.initBackground();
	designer.rules[0].properties.killProperty('vertical-align');
	designer.rules[0].properties.initProperty('width');
	designer.rules[0].properties.initProperty('min-height');
	
	// TextBox H2
	designer.rules[1].properties.initBorder();
	designer.rules[1].properties.initFont();
	designer.rules[1].properties.initMargin();
	designer.rules[1].properties.initCellProp();
	designer.rules[1].properties.initBackground();
	designer.rules[1].properties.killProperty('vertical-align');
	
	// TextBox DIV A
	designer.rules[2].properties.initProperty('color');
	designer.rules[2].properties.initProperty('font-weight');

	// TextBox DIV A:visited
	designer.rules[3].properties.initProperty('color');
	designer.rules[3].properties.initProperty('font-weight');

	// Load necessary CSS files	in preview frame
	loadPreviewStyles(window.frames['demoFrame'].document, 
		tinyMCEPopup.editor.getParam("stylelibrary_previewstyles", 
			tinyMCEPopup.editor.getParam("content_css")));

	// Parse input, get dom pointers, create dom rules.
	designer.init(window.frames['demoFrame'].document, "wysiwyg");

	previewSettings.switchLorem(false, true);
	previewSettings.demoBgInit();
	setDemoClass();
	mcTabs.showTab(document.getElementById('task_style'));
	setTask('style');
	updateDemo();
	detectPaddingH2();
}

function updateDemo()
{
	var output = document.getElementById("hdnCssOutput");
	
	designer.generateOutput();
	designer.updateCssDom();
	output.value = removeNL(designer.getCss());
	
	if(DEBUGJS)
	{
		alert(output.value);
	}
}

// Set the loaded class name on demo table.
function setDemoClass()
{
	var styleName = getStyleName();
	var target = window.frames['demoFrame'].document;

	jQuery("div[id^=demoTextBox1]",target).attr('class',styleName);
}

// Add or remove negative margins for the H2 to reset DIV's padding.
function togglePaddingH2(remove)
{
	if(remove)
	{
		// Remove H2's padding using negative margins.
		var top = designer.rules[0].getProperty('padding-top');
		var left = designer.rules[0].getProperty('padding-left');
		var right = designer.rules[0].getProperty('padding-right');
		
		// Negative
		top = (top == '' ? '0' : '-' + top);
		left = (left == '' ? '0' : '-' + left);
		right = (right == '' ? '0' : '-' + right);
		
		designer.rules[1].setProperty('margin-top', top);
		designer.rules[1].setProperty('margin-left', left);
		designer.rules[1].setProperty('margin-right', right);
	}
	else
	{
		// Eliminate H2's negative margins.
		designer.rules[1].setProperty('margin-top', '');
		designer.rules[1].setProperty('margin-left', '');
		designer.rules[1].setProperty('margin-right', '');
	}
	updateDemo();
}

function detectPaddingH2()
{
	var chk = document.getElementById("chkRemovePadding");
	var top = designer.rules[1].getProperty('margin-top');
	var left = designer.rules[1].getProperty('margin-left');
	var right = designer.rules[1].getProperty('margin-right');
	
	chk.checked = (top != '' || left != '' || right != '');
}

// ****** EVENTS ********

function taskChanged()
{
	switch(getTask()) {
		case "style":
			BorderPicker.initFromRule(designer.rules[0]);
			fntFont.initFromRule(designer.rules[0]);
			cellProp.initFromRule(designer.rules[0]);
			break;
		case "heading":
			BorderPicker.initFromRule(designer.rules[1]);
			fntFont.initFromRule(designer.rules[1]);
			cellProp.initFromRule(designer.rules[1]);
			break;
	}
}
